﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace ScrollingA2DBackground
{
    class Sprite : Microsoft.Xna.Framework.Game
    {
        private SpriteBatch sprite;
        private Texture2D[] punch = new Texture2D[6];
        private Texture2D[] stand = new Texture2D[2];
        private Texture2D[] walk = new Texture2D[8];
        private Texture2D[] die = new Texture2D[6];
        private Texture2D[] power = new Texture2D[4];

        private Texture2D[] monster = new Texture2D[13];

        private int textureStartX = 0;

        private bool walkingRight = false;
        private bool walkingLeft = false;
        private bool punchingRight = false;
        private bool punchingLeft = false;
        private bool dyingRight = false;
        private bool dyingLeft = false;
        private bool poweringRight = false;
        private bool poweringLeft = false;
        private bool standingRight = false;
        private bool standingLeft = false;

        private bool rightOrientation = true;


        private int spriteXPos = 0;
        private int spriteYPos = 500;
        private int spriteSpeedX = 3;
        private int spriteSpeedY = 3;

        private int enemyXPos = 600;
        private int enemyYPos = 470;
        private int enemySpeedX = 3;
        private int enemySpeedY = 3;

        int height;
        int width;

        int middle;
        int[] position = new int[100];
        Rectangle[] moves = new Rectangle[10];

        Rectangle r1;
        Rectangle r2;

        GraphicsDeviceManager graphics;
        ContentManager content;

        private Texture2D currentTexture;
        private Texture2D currentMonster;

        public Sprite(GraphicsDeviceManager g, ContentManager c)
        {
            graphics = g;
            content = c;
            r1 = new Rectangle(spriteXPos, spriteYPos, 100, 100);
            r2 = new Rectangle(enemyXPos, enemyYPos, 150, 150);
        }

        public void updatePositions(int x1, int y1, int x2, int y2)
        {
            r1 = new Rectangle(x1, y1, 100, 100);
            r2 = new Rectangle(x2, y2, 150, 150);
        }

        public void LoadGraphicsContent()
        {
            sprite = new SpriteBatch(this.graphics.GraphicsDevice);
            currentTexture = content.Load<Texture2D>("Resources/stand0") as Texture2D;
            currentMonster = content.Load<Texture2D>("Resources/monster0") as Texture2D;

            //Load the image into the texture object
            for (int i = 0; i < punch.Length; i++)
            {
                punch[i] = content.Load<Texture2D>("Resources/punch" + i) as Texture2D;
            }

            for (int i = 0; i < stand.Length; i++)
            {
                stand[i] = content.Load<Texture2D>("Resources/stand" + i) as Texture2D;
            }

            for (int i = 0; i < walk.Length; i++)
            {
                walk[i] = content.Load<Texture2D>("Resources/walk" + i) as Texture2D;
            }

            for (int i = 0; i < die.Length; i++)
            {
                die[i] = content.Load<Texture2D>("Resources/die" + i) as Texture2D;
            }

            for (int i = 0; i < power.Length; i++)
            {
                power[i] = content.Load<Texture2D>("Resources/powerup" + i) as Texture2D;
            }

            for (int i = 0; i < monster.Length; i++)
            {
                monster[i] = content.Load<Texture2D>("Resources/monster" + i) as Texture2D;
            }

           // height = texture.Height;
           // width = texture.Width;
        }

        public void update(GameTime gameTime)
        {
            //Check for keyboard input
            CheckKeyboardInput();

             elapsedTime = elapsedTime + gameTime.ElapsedGameTime.Milliseconds;
             if (elapsedTime > 50)
             {
                 elapsedTime = 0;
                 //Update the Sprite
                 updateSprite();

                 //
                 checkEnemy();
                 testCollision();
             }
        }

        public void testCollision()
        {
            if (r1.Intersects(r2))
            {
                dyingLeft = true;
            }
        }

        public void checkEnemy()
        {
            Random r = new Random();
            int next = r.Next(4);

            if (next == 1)
            {
                currentMonster = monster[0];
            }
            if (next == 2)
            {
                currentMonster = monster[3];
            }
            enemyXPos -= enemySpeedX;
        }

        double elapsedTime = 0.0;
        int k = 0;
        int l = 4;
        int m = 0;
        int n = 3;
        
        public void updateSprite()
        {
               if (walkingRight)
                {
                    if (k >= 4)
                    {
                        k = 0;
                    }
                    else
                    {
                        currentTexture = walk[k];
                        k++;
                    }
                    walkingRight = false;
                }

                if (walkingLeft)
                {
                    if (l >= 8)
                    {
                        l = 4;
                    }
                    else
                    {
                        currentTexture = walk[l];
                        l++;
                    }
                    walkingLeft = false;
                }

                if (punchingRight)
                {
                    if (m >= 3)
                    {
                        m = 0;
                    }
                    else
                    {
                        currentTexture = punch[m];
                        m++;
                    }
                    punchingRight = false;
                }

                if (punchingLeft)
                {
                    if (n >= 6)
                    {
                        n = 3;
                    }
                    else
                    {
                        currentTexture = punch[n];
                        n++;
                    }
                    punchingLeft = false;
                }

                if (standingLeft)
                {
                    currentTexture = stand[1];
                    standingLeft = false;
                }

                if (standingRight)
                {
                    currentTexture = stand[0];
                    standingRight = false;
                }

                if (dyingLeft)
                {
                    currentTexture = die[2];
                    dyingLeft = false;
                }
        
            //Check if the sprite has collided with a boundary
            if (checkBoundaryCollision() == true)
            {
                //Move to the next frame of the sprite when a collision occurs
                changeSpriteFrame();
            }
        }

        //Check for keyboard input
        protected void CheckKeyboardInput()
        {
            KeyboardState aKeyboard = Keyboard.GetState();

            //Get the current keys being pressed
            Keys[] aCurrentKeys = aKeyboard.GetPressedKeys();

            //Cycle through all of the keys being pressed and move the sprite accordingly
            for (int aCurrentKey = 0; aCurrentKey < aCurrentKeys.Length; aCurrentKey++)
            {
                //Change the sprite's direction to the left and move it left
                if (aCurrentKeys[aCurrentKey] == Keys.Left)
                {
                    if (spriteSpeedX > 0)
                    {
                        spriteSpeedX *= -1;
                    }
                    spriteXPos += spriteSpeedX;
                    walkingLeft = true;
                    rightOrientation = false;
                }

                //Change the sprite's direction to the right and move it right
                // Move right
                else if (aCurrentKeys[aCurrentKey] == Keys.Right)
                {
                    if (spriteSpeedX < 0)
                    {
                        spriteSpeedX *= -1;
                    }
                    spriteXPos += spriteSpeedX;
                    walkingRight = true;
                    rightOrientation = true;
                }

                //Change the sprite's direction to up and move it up
               /* else if (aCurrentKeys[aCurrentKey] == Keys.Up)
                {
                    if (spriteSpeedY > 0)
                    {
                        spriteSpeedY  *= -1;
                    }
                    spriteYPos += spriteSpeedY;
                    //walking = false;
                }

                //Change the sprite's direction to down and move it down
                else if (aCurrentKeys[aCurrentKey] == Keys.Down)
                {
                    if (spriteSpeedY < 0)
                    {
                        spriteSpeedY *= -1;
                    }
                    spriteYPos += spriteSpeedY;
                }*/

                else if (aCurrentKeys[aCurrentKey] == Keys.C)
                {
                    if (rightOrientation == true)
                    {
                        punchingRight = true;
                    }
                    else if (rightOrientation == false)
                    {
                        punchingLeft = true;
                    }
                }

                //Exit the game if the Escape key has been pressed
                else if (aCurrentKeys[aCurrentKey] == Keys.Escape)
                {
                    this.Exit();
                }

                else if(aCurrentKeys[aCurrentKey] == Keys.None)
                {
                    if(rightOrientation == true)
                    {
                        standingRight = true;
                    }
                    else
                    {
                        standingLeft = true;
                    }
                }

            }
        }

        //Check to see if the Sprite has collided with a boundary
        public Boolean checkBoundaryCollision()
        {
            //Determine if a boundary collision occurred
            Boolean aCollision = false;

            //Set the max and min for the X boundaries of the area the sprite can move within
            int aMaxX = Window.ClientBounds.Width - 60;
            int aMinX = 0;

            //Set the max and min for the Y boundaries of the area the sprite can move within
            int aMaxY = Window.ClientBounds.Height - 100;
            int aMinY = 0;

            //Check to see if the sprite has moved out of the allowed area
            if (spriteXPos > aMaxX)
            {
                spriteSpeedX *= -1;
                spriteXPos = aMaxX;
                aCollision = true;
            }
            else if (spriteXPos < aMinX)
            {
                spriteSpeedX *= -1;
                spriteXPos = aMinX;
                aCollision = true;
            }

            if (spriteYPos > aMaxY)
            {
                spriteSpeedY *= -1;
                spriteYPos = aMaxY;
                aCollision = true;
            }
            else if (spriteYPos < aMinY)
            {
                spriteSpeedY *= -1;
                spriteYPos = aMinY;
                aCollision = true;
            }
            return aCollision;
        }

        //Move the sprite to the next frame
        public void changeSpriteFrame()
        {
            if (textureStartX == 0)
            {
                textureStartX = 0;
            }
            else
            {
                textureStartX = 0;
            }
        }

        public void draw(GameTime gameTime)
        {
            // TODO: Add your drawing code here
            //Begin drawing the scene. Using AlphaBlend as the blend mode will use the transparencies of the images drawn.
            sprite.Begin(SpriteBlendMode.AlphaBlend);
           // setMoves(5, 3);
            updatePositions(spriteXPos, spriteYPos, enemyXPos, enemyYPos);

            //Draw the sprite
            sprite.Draw(currentTexture, r1, Color.White);
            sprite.Draw(currentMonster, r2, Color.White);
            sprite.End();
            //sprite.Draw(

            base.Draw(gameTime);
        }
    }
}
